package cn.com.blueInfo.HaiYuan.business.purchase.service.impl;

import cn.com.blueInfo.HaiYuan.business.purchase.entity.PurchaseBase;
import cn.com.blueInfo.HaiYuan.business.purchase.mapper.PurchaseBaseMapper;
import cn.com.blueInfo.HaiYuan.business.purchase.service.PurchaseBaseService;
import cn.com.blueInfo.utils.result.BootstrapTable;
import cn.com.blueInfo.utils.result.ResultInfo;
import cn.com.blueInfo.utils.util.DateUtil;
import cn.com.blueInfo.utils.util.JSONUtil;
import cn.com.blueInfo.utils.util.RequestPayload;
import cn.com.blueInfo.utils.util.UUIDUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 采购单基本信息服务接口具体实现
 * @ClassName: PurchaseBaseServiceImpl
 * @author suxch
 * @date 2020年11月7日  下午2:32:11
 */
@Log4j2
@Service
public class PurchaseBaseServiceImpl implements PurchaseBaseService {

    private static final String logTitle = "采购单基本信息";

    @Autowired
    private PurchaseBaseMapper purchaseBaseMapper;

    /**
     * 分页查询采购单基本数据具体实现
     * @Title: queryPurchaseBaseDataByPage
     * @param request
     * @return BootstrapTable
     */
    @Override
    public BootstrapTable queryPurchaseBaseDataByPage(HttpServletRequest request) {
        BootstrapTable result = new BootstrapTable();
        JSONObject param = RequestPayload.analysis(request);
        log.info("bootstrapTable请求参数" + param);

        Map<String, Object> paramMap = JSON.parseObject(param.toJSONString());

        try {
            Integer count = purchaseBaseMapper.queryPurchaseBaseInfoCount(paramMap);
            List<PurchaseBase> purchaseBaseList = purchaseBaseMapper.queryPurchaseBaseInfo(paramMap);
            result.setTotal(count);
            result.setRows(purchaseBaseList);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            result.setTotal(0);
            result.setRows(null);
        }
        return result;
    }

    /**
     * 新增采购单基本数据具体实现
     * @Title: insertPurchaseBaseData
     * @param request
     * @return ResultInfo
     */
    @Override
    public ResultInfo insertPurchaseBaseData(HttpServletRequest request) {
        ResultInfo result = new ResultInfo();
        String requestData = request.getParameter("requestParam");

        if (JSONUtil.isToJSONObject(requestData)) {
            Date currDate = new Date();
            PurchaseBase purchaseBase = JSON.parseObject(requestData, PurchaseBase.class);
            purchaseBase.setUnid(UUIDUtil.createUUID());
            purchaseBase.setSystemTime(DateUtil.getFormatDateTime(currDate));
            purchaseBase.setModifyTime(DateUtil.getFormatDateTime(currDate));

            try {
                Integer addCount = purchaseBaseMapper.addPurchaseBaseInfo(purchaseBase);
                if (addCount == 1) {
                    result.setResult(true);
                    result.setMessage(logTitle + "添加成功");
                }
            } catch (Exception e) {
                result.setResult(false);
                result.setError(e.getMessage());
                result.setMessage(logTitle + "增加失败，请联系管理员");
                log.error(e.getMessage());
            }
        } else {
            result.setResult(false);
            result.setMessage("参数异常，请联系管理员");
        }
        return result;
    }

    /**
     * 更新采购单基本数据具体实现
     * @Title: updatePurchaseBaseData
     * @param request
     * @return ResultInfo
     */
    @Override
    public ResultInfo updatePurchaseBaseData(HttpServletRequest request) {
        ResultInfo result = new ResultInfo();
        String requestData = request.getParameter("requestParam");

        if (JSONUtil.isToJSONObject(requestData)) {
            Date currDate = new Date();
            PurchaseBase purchaseBase = JSON.parseObject(requestData, PurchaseBase.class);
            purchaseBase.setModifyTime(DateUtil.getFormatDateTime(currDate));
            try {
                Integer count = purchaseBaseMapper.updatePurchaseBaseInfoByUnid(purchaseBase);
                if (count == 1) {
                    result.setResult(true);
                    result.setMessage(logTitle + "更新成功");
                }
            } catch (Exception e) {
                result.setResult(false);
                result.setMessage(logTitle + "更新失败，请联系管理员");
                result.setError(e.getMessage());
                log.error(e.getMessage());
            }
        } else {
            result.setResult(false);
            result.setMessage("参数异常");
        }
        return result;
    }

    /**
     * 删除采购单基本数据具体实现
     * @Title: deletePurchaseBaseData
     * @param request
     * @return ResultInfo
     */
    @Override
    public ResultInfo deletePurchaseBaseData(HttpServletRequest request) {
        ResultInfo result = new ResultInfo();
        String requestData = request.getParameter("requestParam");

        if (JSONUtil.isToJSONObject(requestData)) {
            Date currDate = new Date();
            PurchaseBase purchaseBase = JSON.parseObject(requestData, PurchaseBase.class);
            purchaseBase.setModifyTime(DateUtil.getFormatDateTime(currDate));
            try {
                Integer count = purchaseBaseMapper.updatePurchaseBaseInfoByUnid(purchaseBase);
                if (count == 1) {
                    result.setResult(true);
                    result.setMessage(logTitle + "删除成功");
                }
            } catch (Exception e) {
                result.setResult(false);
                result.setMessage(logTitle + "删除失败，请联系管理员");
                result.setError(e.getMessage());
                log.error(e.getMessage());
            }
        } else {
            result.setResult(false);
            result.setMessage("参数异常");
        }
        return result;
    }

}
